package m3;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws Exception {
        // 连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140"); // wht6.cn
        f.setUsername("admin");
        f.setPassword("admin");
        Channel c = f.newConnection().createChannel();

        // 1定义队列  2定义交换机 3 绑定
        String queue = UUID.randomUUID().toString();
        c.queueDeclare(queue, false, true, true, null);

        c.exchangeDeclare("log", BuiltinExchangeType.FANOUT);

        c.queueBind(queue, "log", "");

        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String s, Delivery delivery) throws IOException {
                String msg = new String(delivery.getBody());
                System.out.println("收到:" + msg);
            }
        };

        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String s) throws IOException {

            }
        };

        //从queue队列消费信息
        c.basicConsume(queue, true, deliverCallback, cancelCallback);

    }
}
